<template>
<base-breadcrumb>
    <designIssueReportingAed use-component @finish="finish" isAudits></designIssueReportingAed>
    <a-card>
        <title-name title="处理"></title-name>
        <a-form-model layout="horizontal" :label-col="{ span:4 }"
                      ref="ruleForm"
                      :model="form"
                      :rules="rules"
                      :wrapper-col="{ span: 14 }">
            <a-row>
                <a-col :span="8">
                    <a-form-model-item ref="name" label="问题处理方式" prop="dealType">
                        <a-radio-group name="radioGroup" v-model="form.dealType" @change="handleChange"
                        :disabled="isTransfer || todoStatus == 1">
                            <a-radio :value="1">
                                本人处理
                            </a-radio>
                            <a-radio :value="2">
                                转办处理
                            </a-radio>
                        </a-radio-group>
                    </a-form-model-item>
                </a-col>
                <template v-if="form.dealType==2">
                    <a-col :span="8" v-if="isTransfer">
                        <a-form-model-item ref="name" label="转办人" prop="name">
                            <a-input v-model="form.transferor" disabled />
                        </a-form-model-item>
                    </a-col>
                    <a-col :span="8">
                        <a-form-model-item ref="name" label="转办给" prop="receiverId">
                            <selectPerson
                                    :disabled="isTransfer || todoStatus == 1"
                                    v-model="form.receiverId"
                                    placeholder="请选择"
                                    :userList="userList"
                                    show-search
                                    @personChanged="personChanged"
                            ></selectPerson>
                        </a-form-model-item>
                    </a-col>
                </template>
                <a-col :span="24" v-if="form.dealType==2 || isTransfer">
                    <a-form-model-item label="转办建议" prop="transSuggestion">
                        <BaseTextarea v-model="form.transSuggestion" placeholder="请输入" :disabled="isTransfer || todoStatus == 1" />
                    </a-form-model-item>
                </a-col>
                <a-col :span="24" v-if="form.dealType==1 || isTransfer">
                    <a-form-model-item label="处理意见" prop="suggestion">
                        <BaseTextarea v-model="form.suggestion" placeholder="请输入"
                        :disabled="todoStatus==1" />
                    </a-form-model-item>
                </a-col>
            </a-row>
        </a-form-model>
        <template v-if="form.dealType==1 || isTransfer">
        <title-name title="解决方案"></title-name>
        <BTableUpload v-model="fileIds" :business-code="businessCode" btnName="上传附件" :disabled="todoStatus==1">
        </BTableUpload>
        </template>
    </a-card>
    <div style="float: right" class="page-btn-right-top">
        <a-button type="primary" @click="confirm" :loading="loading" style="margin-right: 5px" v-if="todoStatus!=1">确认处理</a-button>
        <a-button @click="$router.back()"> 关闭</a-button>
    </div>
</base-breadcrumb>
</template>

<script>
    import designIssueReportingAed from './designIssueReportingAed'
    import { finishBusinessTodo } from '@/api/commons'
    import selectPerson from './selectPerson'
    import { problemReportEdit } from '@/api/design/designRegister'
  export default {
    name: 'designIssueReportingTodo',
    components: {
      designIssueReportingAed,
      selectPerson
    },
    data() {
      return {
        loading: false,
        fileIds: [],
        userList: [],
        businessCode: 'PM18',
        form: {
          dealType: 1, // 处理类型
          transferorId: "", //转办人id
          transferor: '',
          receiverId: '', // 接收转办人
          receiver: '',
          transSuggestion: '', // 转办建议
          suggestion: '', // 处理意见
          suggestFileDTO: [],
          status: ''
        },
        rules: {
          receiverId: [{ required: true, message: '请选择', trigger: 'change' }],
          dealType: [{ required: true, message: '请选择问题处理方式', trigger: 'change' }],
          transSuggestion: [{ required: true, message: '请输入转办建议', trigger: 'blur' }],
          suggestion: [{ required: true, message: '请输入处理意见', trigger: 'blur' }],
        },
        objForm: {},
        isTransfer: false,
        todoStatus: ''
      }
    },
    methods: {
      handleChange() {
        if(this.form.dealType == 2) {
          this.form.suggestion = null
           this.setPerson()
        } else {
          this.form.transSuggestion = null
          this.form.receiverId = null
          this.form.receiver = null
          this.form.transferor = null
          this.form.transferorId = null
        }
      },
      setPerson() {
        this.form.transferor = this.$userInfo().name
        this.form.transferorId = this.$userInfo().employee.userId
      },
        personChanged(val) {
            this.form.receiver = val.employeeName
        },
      finish(info) {
        if(this.$route.query.todoStatus == 1) {
          // this.isTransfer = true
          this.todoStatus = this.$route.query.todoStatus
        }
          this.form= {
              dealType: info.dealType || 1, // 处理类型
              transferorId: info.transferorId, //转办人id
              transferor: info.transferor,
              receiverId: info.receiverId, // 接收转办人
              receiver: info.receiver,
              transSuggestion: info.transSuggestion, // 转办建议
              suggestion: info.suggestion, // 处理意见
              suggestFileDTO: [],
              status: info.status
          }
          if(!info.suggestFileVO) {
            info.suggestFileVO = []
          }
          this.fileIds = info.suggestFile || []
        if(info.dealType == 2) {
          this.isTransfer = true
        }
        this.objForm = info
        this.form.status = info.status
      },
      confirm() {
        this.$refs.ruleForm.validate(async (valid) => {
          if (valid) {
            if(this.form.status == 'IMPLEMENT') {
              this.form.status = 'PAST'
            } else {
              if(this.form.dealType == 2) {
                this.form.status = 'IMPLEMENT'
              } else {
                this.form.status = 'PAST'
              }
            }

            let arr = []
            this.fileIds.forEach(item => {
              arr.push(item.commonId)
            })
            const data = {
              ...this.objForm,
              ...this.form,
                dealUser: this.$userInfo().id,
              id: this.$route.query.bussinessId,
              suggestFileDTO: arr,
              fileListDTO: this.objForm.fileListVO || []
            }
            this.loading = true
            try {
              await finishBusinessTodo({
                id: this.$route.query.id
              })
              await problemReportEdit(data)
              this.loading = false
              this.$message.success("处理成功")
              this.$router.back()
            } catch (e) {
              this.loading = false
            }

          } else {
            this.$message.warning('请填写完所有必填项信息')
          }
        })
      }
    }
  }
</script>

<style scoped lang="less">
/deep/ .BaseBreadcrumb-container {
    padding: 0 0 10px 0!important;
}
/deep/ .content-bx  .content-bx{
    height: auto!important;
    overflow: auto!important;
}
</style>